package com.wondertek.web.controller.sys;

import com.wondertek.common.base.Result;

import com.wondertek.common.util.security.SecurityUtil;
import com.wondertek.service.dto.sys.req.SysMenuPoReq;
import com.wondertek.service.dto.sys.res.SysMenuTypeRes;
import com.wondertek.service.entity.sys.SysMenuPo;
import com.wondertek.service.service.sys.ISysAuthService;
import com.wondertek.service.service.sys.ISysMenuService;
import com.wondertek.service.service.sys.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @author xqs
 */
@RestController
@RequestMapping("/backend/sys/menu")
@Tag(name = "系统菜单管理")
public class SysMenuController{

    @Resource
    private ISysMenuService sysMenuService;

    @Resource
    private ISysAuthService sysAuthService;

    /**
     * 查询所有菜单
     * @return /
     */
    @GetMapping("/findList")
    @Operation(summary = "查询所有菜单")
    public Result<List<SysMenuPo>> findList(){
        return Result.success(sysMenuService.findList());
    }

    /**
     * 查询所有菜单
     * @return /
     */
    @GetMapping("/findAllTree")
    @Operation(summary = "查询所有菜单树形结构(包含按钮)")
    public Result<List<SysMenuPo>> findAllTree(){
        return Result.success(sysMenuService.findAllTree());
    }

    /**
     * 查询所有菜单（树形结构）
     * @return /
     */
    @GetMapping("/findTree")
    @Operation(summary = "查询所有菜单（树形结构）")
    public Result<List<SysMenuPo>> findTree(){
        return Result.success(sysMenuService.findTree());
    }

    /**
     *  用户id查询菜单 (树形结构）
     * @return /
     */
    @GetMapping("/findTreeByUserId")
    @Operation(summary = "用户id查询菜单（树形结构）")
    public Result<List<SysMenuPo>> findTreeByUserId(){
        String userId = SecurityUtil.getLoginUser().getUserId();
        return Result.success(sysMenuService.findTreeByUserId(userId));
    }


    /**
     * 角色id查询菜单 包括全选和非全选区分
     * @return /
     */
    @GetMapping("/findListByRoleIdType")
    @Operation(summary = "角色id查询菜单包括全选和非全选区分")
    public Result<List<SysMenuTypeRes>> findListByRoleIdType(@RequestParam("roleId") String roleId){
        return Result.success(sysMenuService.findListByRoleIdType(roleId));
    }

    /**
     * 角色id查询菜单
     * @return /
     */
    @GetMapping("/findListByRoleId")
    @Operation(summary = "角色id查询菜单")
    public Result<List<SysMenuPo>> findListByRoleId(@RequestParam("roleId") String roleId){
        return Result.success(sysMenuService.findListByRoleId(roleId));
    }

    /**
     * 角色id查询菜单 (树形结构）
     * @return /
     */
    @GetMapping("/findTreeByRoleId")
    @Operation(summary = "角色id查询菜单 (树形结构）")
    public Result<List<SysMenuPo>> findTreeByRoleId(@RequestParam("roleId") String roleId){
        return Result.success(sysMenuService.findTreeByRoleId(roleId));
    }

    /**
     * 新增菜单/权限
     * @param sysMenuPo /
     * @return /
     */
    @PostMapping("/add")
    @Operation(summary = "新增菜单/权限")
    public Result<?> add(@Validated @RequestBody SysMenuPoReq sysMenuPo){
        sysMenuService.add(sysMenuPo);
        return Result.success();
    }

    /**
     * 修改菜单
     * @param sysMenuPoReq /
     * @return /
     */
    @PostMapping("/update")
    @Operation(summary = "修改菜单/权限")
    public Result<?> update(@Validated @RequestBody SysMenuPoReq sysMenuPoReq){
        sysMenuService.update(sysMenuPoReq);
        return Result.success();
    }


    /**
     * 删除菜单
     * @param id /
     * @return /
     */
    @PostMapping("/delete")
    @Operation(summary ="删除菜单/权限")
    public Result<?>  delete(String id){
        sysMenuService.delete(id);
        return  Result.success();
    }

    /**
     * 获取子菜单/按钮权限合集
     * @param id /
     * @return /
     */
    @GetMapping("/getChilrenList")
    @Operation(summary ="获取子菜单/按钮权限合集")
    public Result<List<SysMenuPo>>  getChilrenList(@RequestParam("id")String id,@RequestParam("menuType")String menuType){
        return Result.success(sysMenuService.getChilrenList(id,menuType));
    }


    /**
     * 同步缓存
     *
     * @return /
     */
    @GetMapping("/resetCache")
    @Operation(summary ="同步缓存")
    public Result<?>  resetCache(){
        sysAuthService.initButtonPermission();
        return Result.success();
    }
}
